/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.ChitiethoadonDAO;
import dao.HoadonDAO;
import dao.NguoidungDAO;
import dao.TinhtrangDAO;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import pojo.Chitiethoadon;
import pojo.ChitiethoadonId;
import pojo.Hoadon;
import pojo.Nguoidung;
import pojo.Tinhtrang;

/**
 *
 * @author kydrenw
 */
public class DonHangController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        hsr1.setCharacterEncoding("UTF-8");

        Nguoidung nguoiDung =(Nguoidung) hsr.getSession().getAttribute("nguoiDung");
        if (nguoiDung==null || !NguoidungDAO.laQuanLy(nguoiDung.getTendangnhap()) ) {           
            hsr.setAttribute("tag", "no");
            return new ModelAndView("admin/index");
        }  
        
        String action = hsr.getParameter("action");
        if (action != null) {
            if (action.equals("chitiet")) {
                int mahd = Integer.parseInt(hsr.getParameter("id").toString());
                Hoadon hd = HoadonDAO.layThongTin(mahd);
                hsr.setAttribute("hoadon", hd);
                hsr.setAttribute("dsChiTiet", hd.getChitiethoadons());
                hsr.setAttribute("tag", "donhang_chitiet");
                return new ModelAndView("admin/index");
            }

            if (action.equals("duyet_hoadon")) {
                int mahd = Integer.parseInt(hsr.getParameter("id").toString());
                Hoadon hd = HoadonDAO.layThongTin(mahd);
                HoadonDAO.duyetHoaDons_byAdmin(mahd);
            }

            if (action.equals("huy_hoadon")) {
                int mahd = Integer.parseInt(hsr.getParameter("id").toString());
                Hoadon hd = HoadonDAO.layThongTin(mahd);
                HoadonDAO.huyHoaDons_byAdmin(mahd);
            }

            if (action.equals("huy") || action.equals("duyet")) {
                int mahd = Integer.parseInt(hsr.getParameter("mahd").toString());
                int masp = Integer.parseInt(hsr.getParameter("masp").toString());
                ChitiethoadonId id = new ChitiethoadonId(mahd, masp);
                Chitiethoadon ct = ChitiethoadonDAO.layThongTin(id);
                if (action.equals("huy")) //ChitiethoadonDAO.huyDon(ct);
                {
                    ct.setTinhtrang(new Tinhtrang(10));
                } else if (action.equals("duyet")) {
                    ct.setTinhtrang(new Tinhtrang(8));
                }

                ChitiethoadonDAO.suaDon(ct);
                Hoadon hd = HoadonDAO.layThongTin(mahd);
                hsr.setAttribute("hoadon", hd);
                hsr.setAttribute("dsChiTiet", hd.getChitiethoadons());
                hsr.setAttribute("tag", "donhang_chitiet");
                return new ModelAndView("admin/index");
            }

            if (action.equals("reloadHoadon")) {
                String getDate = hsr.getParameter("date");
                String matt = hsr.getParameter("matt");
                List<Hoadon> ds = HoadonDAO.layDanhSach(getDate,matt);

                hsr1.setContentType("text/plain; charset=UTF-8");
                hsr1.getWriter().write(DataTable(ds));
                hsr1.getWriter().close();
            }
        }

        List<Hoadon> ds = HoadonDAO.layDanhSach();
        List<Tinhtrang> dstt = TinhtrangDAO.layDanhSach(3);
        hsr.setAttribute("dsTinhTrang", dstt);
        hsr.setAttribute("ds", ds);
        hsr.setAttribute("tag", "donhang");


        return new ModelAndView("admin/index");
    }

    private String DataTable(List<Hoadon> ds) {
        String rs = "";
        rs += "<table width='100%'>";
        rs += "<tr>";
        rs += "<td class='tbhead'>Thời gian đặt hàng</td>";
        rs += "<td class='tbhead'>Khách hàng</td>";
        rs += "<td class='tbhead'>Người nhận</td>";
        rs += "<td class='tbhead'>Tổng tiền</td>";
        rs += "<td class='tbhead'>Trạng thái</td>";
        rs += "<td class='tbhead'>Thao tác</td>";
        rs += "</tr>";
        if (ds != null && ds.size() > 0) {
            for (int i = 0; i < ds.size(); i++) {
                Hoadon item = ds.get(i);
                rs += "<tr>";
                rs += "<td class='tbcontent'>" + item.getNgaydh() + "</td>";
                rs += "<td class='tbcontent'>" + item.getNguoidung().getHoten() + "</td>";
                rs += "<td class='tbcontent'>" + item.getNguoinhan() + "</td>";
                rs += "<td class='tbcontent'>" + item.getTongtien() + "</td>";
                rs += "<td class='tbcontent'>" + item.getTinhtrang().getTen() + "</td>";
                rs += "<td class='tbcontent'>";
                rs += "<input title='Chi tiết' type='button' class='btn btnEdit' onclick='window.location=\"donhang.htm?action=chitiet&id=" + item.getMahd() + "\"'/>";
                rs += "<input title='Duyệt' type='button' class='btn btnSubmit'onclick='window.location=\"donhang.htm?action=duyet_hoadon&id=" + item.getMahd() + "\"'/>";
                rs += "<input title='Xóa' type='button' class='btn btnDel' onclick='window.location=\"donhang.htm?action=huy_hoadon&id=" + item.getMahd() + "\"'/>";
                rs += "</td>";
                rs += "</tr>";
            }
        }
        rs += "</table>";
        return rs;
    }
}